﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sample.data;
using Microsoft.Xna.Framework.Content.Pipeline;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Xaml;

namespace Sample.Pipeline
{
    public class LevelImpoter
    {
        [ContentImporter(".xaml", DisplayName = "AngryPig Level Importer")]
        public class LevelImporter : ContentImporter<LevelInfo>
        {
            public override LevelInfo Import(string filename, ContentImporterContext context)
            {
                var level = new LevelInfo();
                var canvas = (Canvas)XamlServices.Load(filename);

                level.Bricks = ((Canvas)canvas.Children[0]).Children.OfType<Rectangle>().Select(rc => new OrientedBox()
                {
                    Rotation = 0,
                    Position = new Microsoft.Xna.Framework.Vector2((float)Canvas.GetLeft(rc), (float)Canvas.GetTop(rc)),
                    Size = new Microsoft.Xna.Framework.Vector2((float)rc.Width, (float)rc.Height),
                }).ToArray();

                level.Birds = ((Canvas)canvas.Children[0]).Children.OfType<Ellipse>().Select(rc =>
                    new Microsoft.Xna.Framework.Vector2((float)Canvas.GetLeft(rc), (float)Canvas.GetTop(rc))
                ).ToArray();

                return level;
            }
        }
    }
}
